package com.tarena.lbs.controller;

import com.tarena.lbs.entity.Merchant;
import com.tarena.lbs.entity.User;
import com.tarena.lbs.service.ApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(value = "API接口", tags = "API接口相关操作")
public class ApiController {

    @Autowired
    private ApiService apiService;

    @GetMapping("/user/{id}")
    @ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    public Map<String, Object> getUser(@ApiParam(value = "用户ID", required = true, example = "1") @PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        User user = apiService.getUserById(id);
        if (user != null) {
            result.put("code", 200);
            result.put("message", "获取成功");
            result.put("data", user);
        } else {
            result.put("code", 404);
            result.put("message", "用户不存在");
        }
        return result;
    }

    @GetMapping("/nearby/merchants")
    @ApiOperation(value = "获取附近商家", notes = "根据用户位置获取附近商家")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "latitude", value = "纬度", required = true, dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "longitude", value = "经度", required = true, dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "distance", value = "距离(千米)", required = false, dataType = "Double", paramType = "query", defaultValue = "5.0")
    })
    public Map<String, Object> getNearbyMerchants(
            @ApiParam(value = "纬度", required = true, example = "39.9042") @RequestParam Double latitude,
            @ApiParam(value = "经度", required = true, example = "116.4074") @RequestParam Double longitude,
            @ApiParam(value = "距离(千米)", defaultValue = "5.0") @RequestParam(defaultValue = "5.0") Double distance) {
        Map<String, Object> result = new HashMap<>();
        List<Merchant> merchants = apiService.getNearbyMerchants(latitude, longitude, distance);
        result.put("code", 200);
        result.put("message", "获取成功");
        result.put("data", merchants);
        return result;
    }

    @PostMapping("/message")
    @ApiOperation(value = "发送消息", notes = "向用户发送消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "message", value = "消息内容", required = true, dataType = "String", paramType = "query")
    })
    public Map<String, Object> sendMessage(
            @ApiParam(value = "用户ID", required = true, example = "1") @RequestParam Long userId,
            @ApiParam(value = "消息内容", required = true, example = "这是一条测试消息") @RequestParam String message) {
        Map<String, Object> result = new HashMap<>();
        boolean success = apiService.sendMessage(userId, message);
        if (success) {
            result.put("code", 200);
            result.put("message", "消息发送成功");
        } else {
            result.put("code", 500);
            result.put("message", "消息发送失败");
        }
        return result;
    }

}
